/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the QtSerialBus module.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of The Qt Company Ltd nor the names of its
**     contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "mainwindow.h"
#include "settingsdialog.h"
#include "ui_mainwindow.h"

#include <QModbusRtuSerialSlave>
#include <QModbusTcpServer>
#include <QRegularExpression>
#include <QRegularExpressionValidator>
#include <QStatusBar>
#include <QUrl>
#include <QDebug>
#include <QTimer>

enum ModbusConnection {
    Serial,
    Tcp
};

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , modbusDevice(nullptr)
{
    ui->setupUi(this);
    QTimer *t_timer = new QTimer;
    for(int i=0;i<=0xD4;i++)
    {
        QLabel *label = new QLabel(QString::number(i,16)+"(D:"+QString::number(i,10)+")");
        ui->gridLayout_W->addWidget(label, i, 0);
        QLineEdit *holdReg_9 = new QLineEdit(this);
        holdReg_9->setObjectName(QString("holdReg_%1").arg(i));
        holdReg_9->placeholderText();
        holdReg_9->setPlaceholderText("Hexadecimal A-F, a-f, 0-9.");
//        holdReg_9->setValidator(new QRegularExpressionValidator(QRegularExpression(QStringLiteral("[0-9a-f]{0,4}"),
//            QRegularExpression::CaseInsensitiveOption), this));
        ui->gridLayout_W->addWidget(holdReg_9, i, 1);
        if(i==0x73) label->setText(label->text()+"(B套机端电压)");
        if(i==0x83) label->setText(label->text()+"(C套机端电压)");
        if(i==0x93) label->setText(label->text()+"(1#功率柜1PT风温)");
        if(i==0xA3) label->setText(label->text()+"(1#功率柜状态)");
        if(i==0xA7) label->setText(label->text()+"(A套调节器状态0)");
        if(i==0xAA) label->setText(label->text()+"(B套调节器状态0)");
        if(i==0xAD) label->setText(label->text()+"(C套调节器状态0)");
        if(i==0xB3) label->setText(label->text()+"(1#功率柜故障)");
        if(i==0xB0) label->setText(label->text()+"(灭磁柜状态)");
        if(i==0xB1) label->setText(label->text()+"(MC板状态)");
        if(i==0xB2) label->setText(label->text()+"(IIU板状态)");
        if(i==0xB7) label->setText(label->text()+"(A套调节器故障0)");
        if(i==0xBA) label->setText(label->text()+"(B套调节器故障0)");
        if(i==0xBD) label->setText(label->text()+"(C套调节器故障0)");
        if(i==0xC0) label->setText(label->text()+"(灭磁柜故障)");
        if(i==0xC1) label->setText(label->text()+"(通讯故障)");
        if(i==0xC2) label->setText(label->text()+"(其它故障)");
    }
    for(int i=215;i<=333;i++)
    {
        QLabel *label = new QLabel(QString::number(i,16)+"(D:"+QString::number(i,10)+")");
        ui->gridLayout_MW->addWidget(label, i, 0);
        QLineEdit *holdReg_9 = new QLineEdit(this);
        holdReg_9->setObjectName(QString("holdReg_%1").arg(i));
        holdReg_9->placeholderText();
        holdReg_9->setPlaceholderText("Hexadecimal A-F, a-f, 0-9.");
        ui->gridLayout_MW->addWidget(holdReg_9, i, 1);
        if(i==230)
            connect(holdReg_9, &QLineEdit::textChanged, this, [ t_timer](){t_timer->start(100);});
        if(i==0x146) label->setText(label->text()+"(A套调节器状态2)");
        if(i==0x147) label->setText(label->text()+"(B套调节器状态2)");
        if(i==0x149) label->setText(label->text()+"(A套调节器故障3)");
        if(i==0x14A) label->setText(label->text()+"(B套调节器故障3)");
    }
    for(int i=0x40E0+355;i<=0x48F5+355;i++)
    {
        QLabel *label = new QLabel(QString::number(i,16)+"(T"+QString::number(i-355,16)+")");
        ui->gridLayout_A->addWidget(label, i-0x40E0, 0);
        QLineEdit *holdReg_9 = new QLineEdit(this);
        holdReg_9->setObjectName(QString("holdReg_%1").arg(i));
        holdReg_9->placeholderText();
        holdReg_9->setPlaceholderText("Hexadecimal A-F, a-f, 0-9.");
        ui->gridLayout_A->addWidget(holdReg_9, i-0x40E0, 1);

        switch (i-355-0x4000) {
        case 0x149:
            label->setText(label->text()+"(AVR版本号)");
            break;
        case 0x150:
            label->setText(label->text()+"(零起升压设定值)");
            break;
        case 0x15d:
            label->setText(label->text()+"(调差值)");
            break;
        case 0x160:
            label->setText(label->text()+"(手动PID TB1)");
            break;
        case 0x178:
            label->setText(label->text()+"(VF限制值)");
            break;
        case 0x18c:
            label->setText(label->text()+"(定子电流限制长期运行值)");
            break;
        case 0x19d:
            label->setText(label->text()+"(空载长期运行值)");
            break;
        case 0x1a9:
            label->setText(label->text()+"(MinIL SP0)");
            break;
        case 0x1B7:
            holdReg_9->setText(QString("f000"));
            label->setText(label->text()+"(欠励限制 SP0)");
            break;
        case 0x1B8:
            holdReg_9->setText("f400");
            break;
        case 0x1B9:
            holdReg_9->setText("f800");
            break;
        case 0x1BA:
            holdReg_9->setText("fc00");
            break;
        case 0x1c5:
            label->setText(label->text()+"(PosQ SP0)");
            break;
        case 0x1d5:
            label->setText(label->text()+"(PSS TW1)");
            break;
        }

        if(i==0x41FF+355) i=0x4605+355;
        if(i==0x463C+355) i=0x47FF+355;
    }
    for(int i=0x50E0+355;i<=0x58F5+355;i++)
    {
        QLabel *label = new QLabel(QString::number(i,16)+"(T"+QString::number(i-355,16)+")");
        ui->gridLayout_B->addWidget(label, i-0x50E0, 0);
        QLineEdit *holdReg_9 = new QLineEdit(this);
        holdReg_9->setObjectName(QString("holdReg_%1").arg(i));
        holdReg_9->placeholderText();
        holdReg_9->setPlaceholderText("Hexadecimal A-F, a-f, 0-9.");
        ui->gridLayout_B->addWidget(holdReg_9, i-0x50E0, 1);

        switch (i-355-0x5000) {
        case 0x149:
            label->setText(label->text()+"(AVR版本号)");
            break;
        case 0x150:
            label->setText(label->text()+"(零起升压设定值)");
            break;
        case 0x15d:
            label->setText(label->text()+"(调差值)");
            break;
        case 0x160:
            label->setText(label->text()+"(手动PID TB1)");
            break;
        case 0x178:
            label->setText(label->text()+"(VF限制值)");
            break;
        case 0x18c:
            label->setText(label->text()+"(定子电流限制长期运行值)");
            break;
        case 0x19d:
            label->setText(label->text()+"(空载长期运行值)");
            break;
        case 0x1a9:
            label->setText(label->text()+"(MinIL SP0)");
            break;
        case 0x1B7:
            holdReg_9->setText("f000");
            label->setText(label->text()+"(欠励限制 SP0)");
            break;
        case 0x1B8:
            holdReg_9->setText("f400");
            break;
        case 0x1B9:
            holdReg_9->setText("f800");
            break;
        case 0x1BA:
            holdReg_9->setText("fc00");
            break;
        case 0x1c5:
            label->setText(label->text()+"(PosQ SP0)");
            break;
        case 0x1d5:
            label->setText(label->text()+"(PSS TW1)");
            break;
        }
        if(i==0x51FF+355) i=0x5605+355;
        if(i==0x563C+355) i=0x57FF+355;
    }
    for(int i=0x6101+355;i<=0x68F5+355;i++)
    {
        QLabel *label = new QLabel(QString::number(i,16)+"(T"+QString::number(i-355,16)+")");
        ui->gridLayout_C->addWidget(label, i-0x6101, 0);
        QLineEdit *holdReg_9 = new QLineEdit(this);
        holdReg_9->setObjectName(QString("holdReg_%1").arg(i));
        holdReg_9->placeholderText();
        holdReg_9->setPlaceholderText("Hexadecimal A-F, a-f, 0-9.");
        ui->gridLayout_C->addWidget(holdReg_9, i-0x6101, 1);
        if(i==0x6149+355) label->setText(label->text()+"(AVR版本号)");
        if(i==0x6150+355) label->setText(label->text()+"(零起升压设定值)");
        if(i==0x615D+355) label->setText(label->text()+"(调差值)");
        if(i==0x6160+355) label->setText(label->text()+"(手动PID TB1)");
        if(i==0x6178+355) label->setText(label->text()+"(VF限制值)");
        if(i==0x618C+355) label->setText(label->text()+"(定子电流限制长期运行值)");
        if(i==0x619D+355) label->setText(label->text()+"(空载长期运行值)");
        if(i==0x61A9+355) label->setText(label->text()+"(MinIL SP0)");
        if(i==0x61B7+355) label->setText(label->text()+"(欠励限制 SP0)");
        if(i==0x61C5+355) label->setText(label->text()+"(PosQ SP0)");
        if(i==0x61D5+355) label->setText(label->text()+"(PSS TW1)");

        if(i==0x61FF+355) i=0x6605+355;
        if(i==0x663C+355) i=0x67FF+355;
    }
    setupWidgetContainers();
    ui->connectType->setCurrentIndex(0);
    on_connectType_currentIndexChanged(0);

    m_settingsDialog = new SettingsDialog(this);
    initActions();

//    QTimer *t_timer = new QTimer;///dev/ttyUSB0
    connect(t_timer, &QTimer::timeout, this, [this, t_timer](){
        static int t_counter = 0;
        static bool errorFlag = false;
        if(t_counter<=20)
        {
            if(!setLineEdit(0x63, 400*t_counter)) errorFlag=true;
            if(!setLineEdit(0x64, 200*t_counter)) errorFlag=true;
            if(!setLineEdit(0x73, 400*t_counter)) errorFlag=true;
            if(!setLineEdit(0x74, 200*t_counter)) errorFlag=true;
        }
        else if(t_counter<=30)
        {
            if(!setLineEdit(0x63, 8000+200*(t_counter-20))) errorFlag=true;
            if(!setLineEdit(0x64, 4000+100*(t_counter-20))) errorFlag=true;
            if(!setLineEdit(0x73, 8000+200*(t_counter-20))) errorFlag=true;
            if(!setLineEdit(0x74, 4000+100*(t_counter-20))) errorFlag=true;
        }
        else errorFlag=true;
        t_counter++;
        if(errorFlag){t_counter = 0; t_timer->stop(); errorFlag=false;}
    });

    QTimer *t_timer2 = new QTimer;///dev/ttyUSB0
    connect(t_timer2, &QTimer::timeout, this, [this, t_timer2](){
        static int t_counter = 0;
        if(ui->cb_autoChange->isChecked()==false) return ;
        switch (t_counter) {
        case 0:
            setRegTextValuepp(0x63, 1);
            setRegTextValuepp(0x64, 1);
            setRegTextValuepp(0x65, 1);
            setRegTextValuepp(0x66, 1);
            setRegTextValuepp(0x67, 1);
            setRegTextValuepp(0x68, 1);
            setRegTextValuepp(0x69, 1);
            setRegTextValuepp(0x6A, 1);
            setRegTextValuepp(0x6B, 1);
            setRegTextValuepp(0x6C, 1);
            break;
        case 1:
            setRegTextValuepp(0x63, -1);
            setRegTextValuepp(0x64, -1);
            setRegTextValuepp(0x65, -1);
            setRegTextValuepp(0x66, -1);
            setRegTextValuepp(0x67, -1);
            setRegTextValuepp(0x68, -1);
            setRegTextValuepp(0x69, -1);
            setRegTextValuepp(0x6A, -1);
            setRegTextValuepp(0x6B, -1);
            setRegTextValuepp(0x6C, -1);
            break;
        }
        t_counter++;t_counter=t_counter%2;
    });
    t_timer2->start(300);


//    QRegularExpression regexp(QStringLiteral("holdReg_230"));
//    const QList<QLineEdit *> qle = findChildren<QLineEdit *>(regexp);
//    if(!qle.empty()){
//        QLineEdit *lineEdit = qle.at(0);
//        lineEdit->setText("22");
//        lineEdit->setEnabled(false);
//        connect(lineEdit, &QLineEdit::textChanged, this, [ t_timer](){
//            t_timer->start(100);
//        });
//    }
}

MainWindow::~MainWindow()
{
    if (modbusDevice)
        modbusDevice->disconnectDevice();
    delete modbusDevice;

    delete ui;
}

bool MainWindow::setLineEdit(uint16_t addr, uint16_t data)
{
    QRegularExpression regexp(QString("holdReg_%1").arg(QString::number(addr, 10)));
    const QList<QLineEdit *> qle = findChildren<QLineEdit *>(regexp);
    if(!qle.empty()){
        QLineEdit *lineEdit = qle.at(0);
        lineEdit->setText(QString::number(data, 16));
        return true;
    }
    else return false;
}

void MainWindow::initActions()
{
    ui->actionConnect->setEnabled(true);
    ui->actionDisconnect->setEnabled(false);
    ui->actionExit->setEnabled(true);
    ui->actionOptions->setEnabled(true);

    connect(ui->actionConnect, &QAction::triggered,
            this, &MainWindow::on_connectButton_clicked);
    connect(ui->actionDisconnect, &QAction::triggered,
            this, &MainWindow::on_connectButton_clicked);

    connect(ui->actionExit, &QAction::triggered, this, &QMainWindow::close);
    connect(ui->actionOptions, &QAction::triggered, m_settingsDialog, &QDialog::show);
}

void MainWindow::on_connectType_currentIndexChanged(int index)
{
    if (modbusDevice) {
        modbusDevice->disconnect();
        delete modbusDevice;
        modbusDevice = nullptr;
    }

    ModbusConnection type = static_cast<ModbusConnection> (index);
    if (type == Serial) {
        modbusDevice = new QModbusRtuSerialSlave(this);
    } else if (type == Tcp) {
        modbusDevice = new QModbusTcpServer(this);
        if (ui->portEdit->text().isEmpty())
            ui->portEdit->setText(QLatin1Literal("127.0.0.1:502"));
    }
    ui->listenOnlyBox->setEnabled(type == Serial);

    if (!modbusDevice) {
        ui->connectButton->setDisabled(true);
        if (type == Serial)
            statusBar()->showMessage(tr("Could not create Modbus slave."), 5000);
        else
            statusBar()->showMessage(tr("Could not create Modbus server."), 5000);
    } else {
        QModbusDataUnitMap reg;
        reg.insert(QModbusDataUnit::HoldingRegisters, { QModbusDataUnit::HoldingRegisters, 0, 0x68FF+355+1 });

        modbusDevice->setMap(reg);

        connect(modbusDevice, &QModbusServer::dataWritten,
                this, &MainWindow::updateWidgets);
        connect(modbusDevice, &QModbusServer::stateChanged,
                this, &MainWindow::onStateChanged);
        connect(modbusDevice, &QModbusServer::errorOccurred,
                this, &MainWindow::handleDeviceError);

        connect(ui->listenOnlyBox, &QCheckBox::toggled, this, [this](bool toggled) {
            if (modbusDevice)
                modbusDevice->setValue(QModbusServer::ListenOnlyMode, toggled);
        });
        emit ui->listenOnlyBox->toggled(ui->listenOnlyBox->isChecked());
        connect(ui->setBusyBox, &QCheckBox::toggled, this, [this](bool toggled) {
            if (modbusDevice)
                modbusDevice->setValue(QModbusServer::DeviceBusy, toggled ? 0xffff : 0x0000);
        });
        emit ui->setBusyBox->toggled(ui->setBusyBox->isChecked());

        setupDeviceData();
    }
}

void MainWindow::handleDeviceError(QModbusDevice::Error newError)
{
    if (newError == QModbusDevice::NoError || !modbusDevice)
        return;

    statusBar()->showMessage(modbusDevice->errorString(), 5000);
}

/*
 * 设置文本数值，自动修改寄存器数值
*/
void MainWindow::setRegTextValue(int addr, int value)
{
    QList<QLineEdit *> qle2 = findChildren<QLineEdit *>(QString( "holdReg_%1").arg(addr));
    qle2[0]->setText(QString::number(value,16));
}

void MainWindow::setRegTextValuepp(int addr, int value)
{
    QList<QLineEdit *> qle2 = findChildren<QLineEdit *>(QString( "holdReg_%1").arg(addr));
    int value2 = qle2[0]->text().toInt(nullptr,16);
    if(value2>=0) value2+=value;
    qle2[0]->setText(QString::number(value2,16));
}

void MainWindow::on_connectButton_clicked()
{
    bool intendToConnect = (modbusDevice->state() == QModbusDevice::UnconnectedState);

    statusBar()->clearMessage();

    if (intendToConnect) {
        if (static_cast<ModbusConnection> (ui->connectType->currentIndex()) == Serial) {
            modbusDevice->setConnectionParameter(QModbusDevice::SerialPortNameParameter,
                ui->portEdit->text());
            modbusDevice->setConnectionParameter(QModbusDevice::SerialParityParameter,
                m_settingsDialog->settings().parity);
            modbusDevice->setConnectionParameter(QModbusDevice::SerialBaudRateParameter,
                m_settingsDialog->settings().baud);
            modbusDevice->setConnectionParameter(QModbusDevice::SerialDataBitsParameter,
                m_settingsDialog->settings().dataBits);
            modbusDevice->setConnectionParameter(QModbusDevice::SerialStopBitsParameter,
                m_settingsDialog->settings().stopBits);
        } else {
            const QUrl url = QUrl::fromUserInput(ui->portEdit->text());
            modbusDevice->setConnectionParameter(QModbusDevice::NetworkPortParameter, url.port());
            modbusDevice->setConnectionParameter(QModbusDevice::NetworkAddressParameter, url.host());
        }
        modbusDevice->setServerAddress(ui->serverEdit->text().toInt());
        if (!modbusDevice->connectDevice()) {
            statusBar()->showMessage(tr("Connect failed: ") + modbusDevice->errorString(), 5000);
        } else {
            ui->actionConnect->setEnabled(false);
            ui->actionDisconnect->setEnabled(true);
        }
    } else {
        modbusDevice->disconnectDevice();
        ui->actionConnect->setEnabled(true);
        ui->actionDisconnect->setEnabled(false);
    }
}

void MainWindow::onStateChanged(int state)
{
    bool connected = (state != QModbusDevice::UnconnectedState);
    ui->actionConnect->setEnabled(!connected);
    ui->actionDisconnect->setEnabled(connected);

    if (state == QModbusDevice::UnconnectedState)
        ui->connectButton->setText(tr("Connect"));
    else if (state == QModbusDevice::ConnectedState)
        ui->connectButton->setText(tr("Disconnect"));
}

void MainWindow::setRegister(const QString &value)
{
    if (!modbusDevice)
        return;

    const QString objectName = QObject::sender()->objectName();
    if (registers.contains(objectName)) {
        bool ok = true;
        const int id = QObject::sender()->property("ID").toInt();
        if (objectName.startsWith(QStringLiteral("inReg")))
            ok = modbusDevice->setData(QModbusDataUnit::InputRegisters, id, value.toInt(&ok, 16));
        else if (objectName.startsWith(QStringLiteral("holdReg")))
            ok = modbusDevice->setData(QModbusDataUnit::HoldingRegisters, id, value.toInt(&ok, 16));

        if (!ok)
            statusBar()->showMessage(tr("Could not set register: ") + modbusDevice->errorString(),5000);
    }
}

void MainWindow::updateWidgets(QModbusDataUnit::RegisterType table, int address, int size)
{
    for (int i = 0; i < size; ++i) {
        quint16 value;
        QString text;
        switch (table) {/*
        case QModbusDataUnit::Coils:
            modbusDevice->data(QModbusDataUnit::Coils, address + i, &value);
            coilButtons.button(address + i)->setChecked(value);
            break;*/
        case QModbusDataUnit::HoldingRegisters:
            modbusDevice->data(QModbusDataUnit::HoldingRegisters, address + i, &value);
            registers.value(QStringLiteral("holdReg_%1").arg(address + i))->setText(text
                .setNum(value, 16));
            break;
        default:
            break;
        }
    }
}

// -- private

void MainWindow::setupDeviceData()
{
    if (!modbusDevice)
        return;

    /*
    for (int i = 0; i < coilButtons.buttons().count(); ++i)
        modbusDevice->setData(QModbusDataUnit::Coils, i, coilButtons.button(i)->isChecked());

    for (int i = 0; i < discreteButtons.buttons().count(); ++i) {
        modbusDevice->setData(QModbusDataUnit::DiscreteInputs, i,
            discreteButtons.button(i)->isChecked());
    }*/

    bool ok;
    for (QLineEdit *widget : qAsConst(registers)) {
        if (widget->objectName().startsWith(QStringLiteral("inReg"))) {
            modbusDevice->setData(QModbusDataUnit::InputRegisters, widget->property("ID").toInt(),
                widget->text().toInt(&ok, 16));
        } else if (widget->objectName().startsWith(QStringLiteral("holdReg"))) {
            modbusDevice->setData(QModbusDataUnit::HoldingRegisters, widget->property("ID").toInt(),
                widget->text().toInt(&ok, 16));
        }
    }
}

void MainWindow::setupWidgetContainers()
{
    coilButtons.setExclusive(false);
    discreteButtons.setExclusive(false);

    QRegularExpression regexp(QStringLiteral("coils_(?<ID>\\d+)"));

    regexp.setPattern(QLatin1String("holdReg_(?<ID>\\d+)"));
    const QList<QLineEdit *> qle = findChildren<QLineEdit *>(regexp);
    for (QLineEdit *lineEdit : qle) {
        registers.insert(lineEdit->objectName(), lineEdit);
        lineEdit->setProperty("ID", regexp.match(lineEdit->objectName()).captured("ID").toInt());
        lineEdit->setValidator(new QRegularExpressionValidator(QRegularExpression(QStringLiteral("[0-9a-f]{0,4}"),
            QRegularExpression::CaseInsensitiveOption), this));
        connect(lineEdit, &QLineEdit::textChanged, this, &MainWindow::setRegister);
//        if(lineEdit.is) lineEdit->setText("0");
    }
//    QList<QLineEdit *> qle2 = findChildren<QLineEdit *>(QString( "holdReg_%1").arg(QString("AB").toInt(nullptr, 16)));
//    qle2[0]->setText("80");
//    qle2 = findChildren<QLineEdit *>(QString( "holdReg_%1").arg(QString("A8").toInt(nullptr, 16)));
//    qle2[0]->setText("80");
    //AB通道自动
    setRegTextValue(0xA8, 0x80);
    setRegTextValue(0xAB, 0x80);
    //A通道主用
    //灭磁开关位置合
    setRegTextValue(0xA7, 0x1020);
    setRegTextValue(0xAA, 0x1000);
    //灭磁开关位置合
    setRegTextValue(0xB0, 0x2);

    setRegTextValue(0x146, 0x4200);
    setRegTextValue(0x68, 0x2810);
    setRegTextValue(0x69, 0x1488);

}


void MainWindow::on_pushButton_clicked()
{
    setRegTextValue(0xA7, 0x1020);
    setRegTextValue(0xAA, 0x1000);
}

void MainWindow::on_pushButton_2_clicked()
{
    setRegTextValue(0xA7, 0x1000);
    setRegTextValue(0xAA, 0x1020);
}
